package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.pojo.User;
import com.zjitc.service.UserService;
import java.util.Date;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/29 15:04
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  /**
   * 登录
   * @param username
   * @param password
   * @param session
   * @return
   */
  @RequestMapping("/login")
  @ResponseBody
  public ServerResponse login(
      @RequestParam("username") String username,
      @RequestParam("password") String password,
      HttpSession session)
  {
    ServerResponse serverResponse=userService.login(username,password);
    if(serverResponse.Success()){
      session.setAttribute("user",serverResponse.getData());
    }
    return serverResponse;
  }

  /**
   * 注册
   * @param user
   * @return
   */
  @RequestMapping("/register")
  @ResponseBody
  public ServerResponse register(@ModelAttribute User user){
    ServerResponse serverResponse=userService.register(user);
    return serverResponse;
  }

  /**
   * 检查用户名是否有效
   * @param str
   * @return
   */
  @RequestMapping("/check_valid")
  @ResponseBody
  public ServerResponse check_valid(@RequestParam("str") String str) {
    ServerResponse<User> response = userService.check_valid(str);
    return response;
  }

  /**
   * 获取登录用户信息
   * @param session
   * @return
   */
  @RequestMapping("/get_user_info")
  @ResponseBody
  public ServerResponse get_user_info(HttpSession session){
    User currentUser = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.get_user_info(currentUser.getUsername());
    if (currentUser == null){
      return ServerResponse.createError("用户未登录，需要先登录");
    }
    return response;
  }

  /**
   * 提交问题答案
   * @param username
   * @param question
   * @param answer
   * @return
   */
  @RequestMapping("/forget_check_answer")
  @ResponseBody
  public ServerResponse forget_check_answer(String username, String question, String answer){
    ServerResponse<User> response = userService.checkQuestionAndAnswer(username,question,answer);
    return response;
  }

  /**
   * 忘记密码的重设密码
   * @param username
   * @param passwordNew
   * @param forgetToken
   * @return
   */
  @RequestMapping("/forget_reset_password")
  @ResponseBody
  public ServerResponse forget_reset_password(String username, String passwordNew, String forgetToken){
    ServerResponse<User> response = userService.forgetResetPassword(username,passwordNew,forgetToken);
    return response;
  }

  /**
   * 忘记密码
   * @param username
   * @return
   */
  @RequestMapping("/forget_get_question")
  @ResponseBody
  public ServerResponse forget_get_question(String username){
    ServerResponse<User> response = userService.forget_get_question(username);
    return response;
  }

  /**
   * 登录中状态重置密码
   * @param session
   * @param passwordOld
   * @param passwordNew
   * @return
   */
  @RequestMapping("/reset_password")
  @ResponseBody
  public ServerResponse reset_password(HttpSession session, String passwordOld, String passwordNew){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.reset_password(passwordOld,passwordNew,user);
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  /**
   * 登录状态更新个人信息
   * @param session
   * @param email
   * @param question
   * @param answer
   * @param phone
   * @return
   */
  @RequestMapping("/update_information")
  @ResponseBody
  public ServerResponse update_information(HttpSession session, String email, String question, String answer, String phone){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.update_information(email,question,answer,phone,user);
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  /**
   * 获取当前登录用户的详细信息，并强制登录
   * @param session
   * @return
   */
  @RequestMapping("/get_information")
  @ResponseBody
  public ServerResponse get_information(HttpSession session){
    User user = (User) session.getAttribute("user");
    ServerResponse<User> response = userService.get_information(user.getUsername());
    if(user == null){
      return ServerResponse.createError("用户未登录");
    }
    return response;
  }

  /**
   * 退出登录
   * @param session
   * @return
   */
  @RequestMapping("/logout")
  @ResponseBody
  public ServerResponse logout(HttpSession session){
    session.removeAttribute("user");
    return ServerResponse.createSuccess("成功退出");
  }
}
